<?php
class SettingController extends AppController
{
    public $name = 'Setting';
    public $helpers = array('Html', 'Form');
    public $components = array('Session');
    public $uses = array(
        'tbl_account', 'tbl_character', 'tbl_billing_log', 'tbl_gift',
        'tbl_gift_item_info', 'tbl_gem_log', 'tbl_block_log'
    );

    public function setting_index()
    {
        if(!CakeSession::check('tbl_auth'))
        {
            if(!empty($this->passedArgs['id']))
            {
                $this->redirect(array(
                    'controller' => 'User',
                    'action' => 'user_select',
                    'id' => $this->passedArgs['id'],
                    'search_value' => $this->passedArgs['search_value'],
                ));
            }

            $this->redirect(array(
                'controller' => 'User',
                'action' => 'user_list',
            ));
        }

        $id = $this->passedArgs['id'];
        $user_character_list = $this->tbl_character->get_character($id);
        $gift_item_option = $this->__gift_item_option();

        if(!empty($this->passedArgs['char_serial']))
        {
            $char_serial = $this->passedArgs['char_serial'];
        }

        if(!empty($this->data['gift_item']))
        {
            $gift_item = $this->data['gift_item'];
            if($gift_item['gift'] == 'item')
            {
                $gift_data['char_serial'] = $gift_item['char_serial'];
                $gift_data[$gift_item['gift']] = $gift_item['item'];
                $gift_data['item_count'] = $gift_item['item_ea'];
            }
            if($gift_item['gift'] == 'gold' || $gift_item['gift'] == 'gem')
            {
                if(!empty($gift_item['ea']))
                {
                    $gift_data['char_serial'] = $gift_item['char_serial'];
                    $gift_data[$gift_item['gift']] = $gift_item['ea'];
                }
                else
                {
                    $send_err_msg = __(LANG_EMPTY);
                }
            }

            if(empty($send_err_msg))
            {
                $gift_data['message'] = $gift_item['message'];

                $giftArray = giftArray($gift_data);
                if($this->__gift_send($giftArray))
                {
                    $send_msg = __(LANG_SEND_SUCCESS);
                }
                else
                {
                    $send_err_msg = __(LANG_SEND_FAIL);
                }
            }
        }

        if(!empty($this->data['billing_log']))
        {
            $billing = $this->data['billing_log'];

            if(nullCheck($billing))
            {
                $billing_err_msg = __(LANG_EMPTY);
            }
            else
            {
                if(!dateCheck($billing['reg_date']))
                {
                    $billing_err_msg = __('The date format must be!');
                }
                else
                {
                    $billing['gem'] = cashAndGem($billing['gem']);
                    $billing_log = billingLogArray($billing);
                    $gem_log = gemLogArray($billing);

                    $this->tbl_billing_log->create();
                    $this->tbl_billing_log->save($billing_log);

                    $this->tbl_gem_log->create();
                    $this->tbl_gem_log->save($gem_log);

                    $account = $this->tbl_account->get_account($billing['a2x_id']);
                    $account_gem = array(
                        'a2x_id' => $account['tbl_account']['a2x_id'],
                        'gem' => $account['tbl_account']['gem'] + $billing['gem']['gem']
                    );

                    $this->tbl_account->save($account_gem);

                    $billing_msg = __(LANG_SEND_SUCCESS);
                }
            }
        }

        if( !empty($this->data['block_log']) )
        {

            $block_info = $this->data['block_log'];
            $account = $this->tbl_block_log->get_account($block_info['a2x_id']);

            if( $account > 0 )
            {
                $err_msg = __('A user already exists!');
            }
            else
            {
                if( !dateCheck($block_info['start_date']) || !dateCheck($block_info['end_date']) )
                {
                    $err_msg = __('The date format must be!');
                }
                else
                {
                    $block_log = blockLogArray($block_info);
                    $this->tbl_block_log->create();
                    $this->tbl_block_log->save($block_log);

                    $sus_msg = __('Setting Succeeded!!');
                }
            }
        }

        $this->set(compact('user_character_list', 'gift_item_option', 'char_serial', 'send_msg', 'send_err_msg', 'billing_msg', 'billing_err_msg', 'err_msg', 'sus_msg'));
    }

    private function __gift_item_option()
    {
        $gift_item_list = $this->tbl_gift_item_info->get_gift_item();

        foreach($gift_item_list as $item)
        {
            $gift_item[$item['tbl_gift_item_info']['item_id']] = $item['tbl_gift_item_info']['item_name'];
        }

        return $gift_item;
    }

    private  function __gift_send($_gift_info)
    {
        $this->tbl_gift->create();

        if($this->tbl_gift->save($_gift_info))
        {
            return true;
        }

        return false;
    }
}